﻿#define _CRT_SECURE_NO_WARNINGS
#include"Stack.h"
//初始化栈
void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = NULL;
	ps->top = -1;
}
void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
//栈顶
	void STPush(ST * ps, STDatatype x)
	{
		assert(ps);

		// ˣ 
		if (ps->top == ps->capacity)
		{
			int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
			STDatatype* tmp = (STDatatype*)realloc(ps->a, newcapacity * sizeof(STDatatype));
			if (tmp == NULL)
			{
				perror("realloc fail");
				return;
			}

			ps->a = tmp;
			ps->capacity = newcapacity;
		}

		ps->a[ps->top] = x;
		ps->top++;
	}

//获取栈顶元素
	void STPop(ST* ps)
	{
		assert(ps);
		assert(!STEmpty(ps));//断言栈非空

		ps->top--;
	}

 //计算栈大小的函数
	int STSize(ST* ps)
	{
		assert(ps);

		return ps->top;
	}

	//判断栈是否为空的函数
	bool STEmpty(ST* ps)
	{
		assert(ps);
		return ps->top == 0;
	}
